/*
 * OrganController.java
 *
 * Copyright 2012 the original author or authors(ninglong).
 *
 * Licensed under the org.geek.organ.web, Version 1.0 (the "License");
 *
 */
package org.geek.organ.web;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.geek.common.base.BaseController;
import org.geek.common.entity.ColumnModel;
import org.geek.common.entity.Criteria;
import org.geek.common.entity.ExtGridReturn;
import org.geek.common.entity.ExtPager;
import org.geek.common.entity.ExtReturn;
import org.geek.common.entity.ExtTree;
import org.geek.common.entity.Menu;
import org.geek.common.util.TreeUtil;
import org.geek.domain.Organ;
import org.geek.organ.service.IOrganService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p></p>
 * @author ninglong bhd2973@qq.com
 * @version V1.0
 */
@Controller
@RequestMapping("/organ/")
public class OrganController extends BaseController{

	private static final Logger logger = LoggerFactory.getLogger(OrganController.class);
	
	@Resource
	private IOrganService organService;
	
	@RequestMapping(value="initPage.jhtml",method=RequestMethod.GET)
	@Override
	public String initPage(HttpServletRequest request, ModelMap model) {
		List<ColumnModel> list = new ArrayList<ColumnModel>();
		list.add(new ColumnModel("机构ID","id",true,true));
		list.add(new ColumnModel("机构名称","organName",false,false));
		list.add(new ColumnModel("上级机构","parentName",false,false));
		list.add(new ColumnModel("上级机构Id","parentId",true,false));
		model.put("columnModel", list);
		model.put("parentWinId", request.getParameter("parentWinId"));
		model.put("winId", request.getParameter("winId"));
		
		log(request,logger,"访问机构管理");
		
		return "page/organ";
	}
	
	@RequestMapping(value="getDataList.jhtml",method=RequestMethod.POST)
	@ResponseBody
	public Object getDataList(ExtPager pager){
		Criteria criteria = new Criteria();
		List<Organ> organs = organService.getOrganByCriteria(criteria);
		int total = organService.countOrganByCriteria(criteria);
		// 设置分页信息
		if (pager.getLimit() != null && pager.getStart() != null) {
			criteria.setEnd(pager.getLimit() + pager.getStart());
			criteria.setStart(pager.getStart());
		}
		List<Organ> list = organService.getOrganByCriteria(criteria);
		for(Organ organ : list){
			for(Organ o : organs){
				if(organ.getParentId()==o.getId()){
					organ.setParentName(o.getOrganName());
				}
			}
		}
		return new ExtGridReturn(total, list);
	}
	
	@RequestMapping(value="getOrganTree.jhtml",method = RequestMethod.POST)
	@ResponseBody
	public List<ExtTree> getOrganTree(HttpServletRequest request){
		String organId = request.getParameter("id");
		List<Menu> list = organService.getOrgans2Menu();
		TreeUtil treeMenu = new TreeUtil(list, Integer.valueOf(organId));
		return treeMenu.getTreeJson().getChildren();
	}
	
	@RequestMapping(value="addOrgan.jhtml",method=RequestMethod.POST)
	@ResponseBody
	public Object addOrgan(HttpServletRequest request,Organ organ){
		boolean success = true;
		
		if(StringUtils.isEmpty(organ.getOrganName()))success = false;
		
		if(success && organService.addOrgan(organ)<=0){
			success = false;
		}
		String msg = success?"机构添加成功！":"机构添加失败！";
		
		log(request,logger,msg);
		return new ExtReturn(success, msg);
	}
	
	@RequestMapping(value="modOrgan.jhtml",method=RequestMethod.POST)
	@ResponseBody
	public Object modOrgan(HttpServletRequest request,Organ organ){
		boolean success = true;
		
		if(StringUtils.isEmpty(organ.getOrganName()))success = false;
		if(organ.getParentId() == 0) success = false;
		
		if(success && organService.modOrgan(organ)<=0){
			success = false;
		}
		String msg = success?"机构修改成功！":"机构修改失败！";
		log(request,logger,msg);
		return new ExtReturn(success, msg);
	}
	
	@RequestMapping(value="delOrganByOrganId.jhtml",method=RequestMethod.POST)
	@ResponseBody
	public Object delOrganByOrganId(HttpServletRequest request,int id){
		boolean success = true;
		if(organService.delOrganByOrganId(id)<=0){
			success = false;
		}
		String msg = success?"机构删除成功！":"机构删除失败！";
		log(request,logger,msg);
		return new ExtReturn(success, msg);
	}
}